#############################
# LIBRARY CHECK AND INSTALLATION
# 
# Part of replication of:
# Traditional Institutions in Africa, Past and Present
#
# Political Science Research and Methods
#
# By Clara Neupert-Wentz and Carl Müller-Crepon, 2023
#
##########################################


# All libraries needed
libraries <- c(
  "lfe", "stargazer","ggplot2","plyr","dplyr","car"
  )


# Missing libraries
lib.miss <- libraries[!libraries %in% installed.packages()[,"Package"]]

# Print
if(length(lib.miss) > 0){
  print(paste("Installing the following", length(lib.miss), "packages:", 
                    paste(lib.miss, collapse = "; ")))
}

if(length(lib.miss) > 0){
  # Install
  install.packages(lib)
  
  # Message
  fails <- libraries[!libraries %in% installed.packages()[,"Package"]]
  message(paste("Successfully installed", length(lib.miss) - length(fails), "packages."))
  if(length(fails) > 0){
    warning(paste("Failed to install the following", length(fails), "packages:", 
                  paste(fails, collapse = "; ")))
  }
} else {
  message(paste("All packages installed. "))
}
